package com.mingtu.jgspatrol.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.databinding.ItemParkManageBinding;
import com.mingtu.jgspatrol.holder.BaseViewBindingHolder;

public class ParkModuleAdapter extends RecyclerView.Adapter<BaseViewBindingHolder<ItemParkManageBinding>> implements View.OnClickListener {

    private Context context;
    private OnItemClickListener mOnItemClickListener = null;

    private int[] bgArray = {R.mipmap.img_park_module1,  R.mipmap.img_park_module3};
    private int[] imgTextArray = {R.mipmap.icon_park_module1_text,  R.mipmap.icon_park_module3_text};
    private int[] imgPicArray = {R.mipmap.img_park_module1_pic,  R.mipmap.img_park_module3_pic};

    public ParkModuleAdapter(Context context) {
        this.context = context;
    }


    @NonNull
    @Override
    public BaseViewBindingHolder<ItemParkManageBinding> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 实例化展示的view
//        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_park_manage, parent, false);
        ItemParkManageBinding binding = ItemParkManageBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        // 实例化viewholder
        BaseViewBindingHolder holder = new BaseViewBindingHolder(binding);
        holder.itemView.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewBindingHolder<ItemParkManageBinding> holder, int position) {
        ItemParkManageBinding binding = holder.getBinding();

        binding.ivBg.setBackgroundResource(bgArray[position]);
        binding.ivText.setBackgroundResource(imgTextArray[position]);
        binding.ivPic.setBackgroundResource(imgPicArray[position]);

        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);

    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return bgArray.length;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

}
